<!-- src/views/student/StudentProfileSetup.vue -->
<template>
  <div class="student-profile-setup">
    <div class="setup-container">
      <el-card class="setup-card">
        <template #header>
          <div class="card-header">
            <h2>完善个人信息</h2>
            <p>请填写以下信息以完成账户设置</p>
          </div>
        </template>

        <el-form
          :model="profileForm"
          :rules="profileRules"
          ref="profileFormRef"
          label-width="100px"
          class="profile-form"
        >
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="学号" prop="studentNumber">
                <el-input v-model="profileForm.studentNumber" placeholder="请输入学号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="姓名" prop="name">
                <el-input v-model="profileForm.name" placeholder="请输入姓名" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="性别" prop="gender">
                <el-select v-model="profileForm.gender" placeholder="请选择性别" style="width: 100%">
                  <el-option label="男" :value="1" />
                  <el-option label="女" :value="0" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="出生日期" prop="birthDate">
                <el-date-picker
                  v-model="profileForm.birthDate"
                  type="date"
                  value-format="YYYY-MM-DD"
                  placeholder="请选择出生日期"
                  style="width: 100%"
                />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="专业" prop="major">
                <el-input v-model="profileForm.major" placeholder="请输入专业" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="年级" prop="grade">
                <el-input v-model="profileForm.grade" placeholder="请输入年级" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="班级" prop="className">
                <el-input v-model="profileForm.className" placeholder="请输入班级" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系电话" prop="phone">
                <el-input v-model="profileForm.phone" placeholder="请输入联系电话" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="邮箱" prop="email">
                <el-input v-model="profileForm.email" placeholder="请输入邮箱" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="头像" prop="avatar">
                <el-input v-model="profileForm.avatar" placeholder="请输入头像URL" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-form-item label="简历" prop="resume">
            <el-input v-model="profileForm.resume" type="textarea" placeholder="请输入简历信息" />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitProfile" :loading="loading">提交</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useStudentStore } from '../../store/student'
import { useAuthStore } from '../../store/auth'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

const studentStore = useStudentStore()
const authStore = useAuthStore()
const router = useRouter()

const profileFormRef = ref()
const loading = ref(false)

const profileForm = reactive({
  studentNumber: '',
  name: '',
  gender: null,
  birthDate: '',
  major: '',
  grade: '',
  className: '',
  phone: '',
  email: '',
  avatar: '',
  resume: ''
})

const profileRules = {
  studentNumber: [{ required: true, message: '请输入学号', trigger: 'blur' }],
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  gender: [{ required: true, message: '请选择性别', trigger: 'change' }],
  birthDate: [{ required: true, message: '请选择出生日期', trigger: 'change' }],
  major: [{ required: true, message: '请输入专业', trigger: 'blur' }],
  grade: [{ required: true, message: '请输入年级', trigger: 'blur' }],
  className: [{ required: true, message: '请输入班级', trigger: 'blur' }],
  phone: [
    { required: true, message: '请输入联系电话', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ]
}

// 提交个人信息
// 提交个人信息
const submitProfile = async () => {
  if (!profileFormRef.value) return

  await profileFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        await studentStore.registerStudentProfile(profileForm)
        ElMessage.success('个人信息完善成功')
        // 跳转到学生仪表板
        router.push('/student/dashboard')
      } catch (error) {
        ElMessage.error(error.message || '个人信息完善失败')
      } finally {
        loading.value = false
      }
    }
  })
}

// 获取当前用户信息并填充到表单中
const fetchCurrentUser = async () => {
  try {
    const user = await authStore.fetchCurrentUser()
    if (user) {
      profileForm.name = user.username
      profileForm.phone = user.phone || ''
      profileForm.email = user.email || ''
    }
  } catch (error) {
    ElMessage.error('获取用户信息失败')
  }
}

onMounted(() => {
  fetchCurrentUser()
})
</script>

<style scoped>
.student-profile-setup {
  height: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}

.setup-container {
  width: 100%;
  max-width: 800px;
  padding: 20px;
}

.setup-card {
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.card-header {
  text-align: center;
}

.card-header h2 {
  margin: 0 0 10px 0;
  color: #303133;
}

.card-header p {
  margin: 0;
  color: #606266;
}

.profile-form {
  margin-top: 20px;
}
</style>
